/* NOTICE
 
    Jim - the Java Image Manager, copyright (c) 2005 - 2007 Grant Gardner 
 
    Jim is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Jim is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  
    In addition, as a special exception, you are permitted to link this code
    with the 3rd party libraries required to compile this file and to
    distribute a linked combination including them. You must obey the
    GNU General Public License in all respects for all of the code except for
    those 3rd party libraries that are licenced under terms incompatible with the
    GNU General Public License.  
 
 
 NOTICE */

package au.com.lastweekend.jim.model;

import java.awt.Image;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.Icon;

import au.com.lastweekend.jim.imagebase.ImageInfo;
import au.com.lastweekend.jim.io.metadata.Tag;
import au.com.lastweekend.jim.swing.AutoScaleIcon;
import au.com.lastweekend.jim.swing.SwingUtil;

/**
 * 
 * @author grant@lastweekend.com.au
 * @version $Id: ContactSheetImageItem.java,v 1.7 2006/02/20 12:22:44 ggardner Exp $
 */
public class ContactSheetImageItem implements ContactSheetListItem {

    private ImageInfo imageInfo;
    private Icon _icon;
    private Image _thumbnail;
    private static final Icon NO_THUMBNAIL = SwingUtil.createImageIcon("no-thumbnail.png");
    private static final Icon LOST_FILE = SwingUtil.createImageIcon("lost-files.png");

    // private static final Logger LOG =
    // Logger.getLogger(ContactSheetImageItem.class);

    public ContactSheetImageItem(ImageInfo imageInfo) {

        this.imageInfo = imageInfo;
    }

    /**
     * Creates list of contactSheetItems from a list of ImageInfo's Sorted by name..
     * 
     * @param directoryImages
     * @return list of contactSheetItems
     */
    public static List<ContactSheetImageItem> makeList(Collection<ImageInfo> directoryImages) {

        ArrayList<ContactSheetImageItem> l = new ArrayList<ContactSheetImageItem>();

        for (ImageInfo imageInfo : directoryImages) {
            l.add(new ContactSheetImageItem(imageInfo));
        }

        return l;
    }

    public File getFile() {

        return imageInfo.getFile();
    }

    public boolean allowsFileDrop() {

        return false;
    }

    public Icon getIcon() {

        if (!imageInfo.hasThumbnail()) {
            return imageInfo.isLost() ? LOST_FILE : NO_THUMBNAIL;
        }

        Image thumbnail = imageInfo.getThumbnail();

        if (thumbnail != _thumbnail) {
            _thumbnail = thumbnail;
            _icon = new AutoScaleIcon(thumbnail, imageInfo.getThumbnailRotationAngle());
        }

        return _icon != null ? _icon : NO_THUMBNAIL;
    }

    public boolean isImported() {

        return imageInfo.hasImageBaseDirectory();
    }

    public boolean isPreviewAble() {

        return true;
    }

    public List<Tag> getAllTags() {

        return imageInfo.getAllTags();
    }


    public void updateKeywordSelection(String keywordString, boolean selected) {

        imageInfo.updateKeywordSelection(keywordString, selected);

    }

    public ImageInfo getImageInfo() {

        return imageInfo;
    }

    public static Collection<ImageInfo> extractImageInfos(Collection<ContactSheetListItem> selectedItems) {

        HashSet<ImageInfo> result = new HashSet<ImageInfo>();
        for (ContactSheetListItem listItem : selectedItems) {
            if (listItem instanceof ContactSheetImageItem) {
                ContactSheetImageItem contactSheetImageItem = (ContactSheetImageItem) listItem;
                result.add(contactSheetImageItem.getImageInfo());
            }
        }

        return result;
    }

    public int compareTo(ContactSheetListItem i) {

        if (i.getFile().isDirectory()) {
            return 1;
        } else {

            return getFile().getName().toLowerCase().compareTo(i.getFile().getName().toLowerCase());
        }
    }

    @Override
    public boolean equals(Object o) {

        return (o instanceof ContactSheetImageItem && this.getFile().equals(((ContactSheetImageItem) o).getFile()));
    }

    @Override
    public int hashCode() {

        return getFile().hashCode();
    }

    public Set<String> getAllKeywords() {

        return imageInfo.getAllKeywords();
    }

    public int getRotationAngle() {

        return imageInfo.getRotationAngle();
    }

}
